home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / printenv.c < prev    next >
C/C++ Source or Header  |  1991-08-29  |  2KB  |  70 lines

  1. /* printenv -- print all or part of environment
  2.    Copyright (C) 1989, 1991 Free Software Foundation.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: printenv [variable...]
  19.  
  20.    If no arguments are given, print the entire environment.
  21.    If one or more variable names are given, print the value of
  22.    each one that is set, and nothing for ones that are not set.
  23.  
  24.    Exit status:
  25.    0 if all variables specified were found
  26.    1 if not
  27.  
  28.    David MacKenzie and Richard Mlynarik */
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include "system.h"
  33.  
  34. extern char **environ;
  35.  
  36. void
  37. main (argc, argv)
  38.      int argc;
  39.      char **argv;
  40. {
  41.   char **env;
  42.   char *ep, *ap;
  43.   int i;
  44.   int matches = 0;
  45.  
  46.   if (argc == 1)
  47.     {
  48.       for (env = environ; *env; ++env)
  49.     puts (*env);
  50.       exit (0);
  51.     }
  52.  
  53.   for (i = 1; i < argc; ++i)
  54.     {
  55.       for (env = environ; *env; ++env)
  56.     {
  57.       ep = *env;
  58.       ap = argv[i];
  59.       while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
  60.         if (*ep == '=' && *ap == '\0')
  61.           {
  62.         puts (ep + 1);
  63.         ++matches;
  64.         break;
  65.           }
  66.     }
  67.     }
  68.   exit (matches != argc - 1);
  69. }
  70.